package  
{
	import flash.display.JointStyle;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	/**
	 * ...
	 * @author patsup
	 */
	public class LoadMenu extends Menu 
	{
		//menu image parameters
		private const COLOR:int = 0xFFFF80;
		private const WIDTH:int = 800;
		private const HEIGHT:int = 480;
		
		private const MENU_BUTTON_X_STARTLOC:int = 750;			//Button for return back to menu
		//private const MENU_BUTTON_Y_STARTLOC:int = 430;
		//private const LOADGAME_1_BUTTON_X_STARTLOC:int = 120;	//Button for load game 1
		//private const LOADGAME_1_BUTTON_Y_STARTLOC:int = 48;
		//private const LOADGAME_2_BUTTON_X_STARTLOC:int = 485;	//Button for load game 2
		//private const LOADGAME_2_BUTTON_Y_STARTLOC:int = 48;
		//private const LOADGAME_3_BUTTON_X_STARTLOC:int = 120;	//Button for load game 3
		//private const LOADGAME_3_BUTTON_Y_STARTLOC:int = 180;
		//private const LOADGAME_4_BUTTON_X_STARTLOC:int = 485;	//Button for load game 4
		//private const LOADGAME_4_BUTTON_Y_STARTLOC:int = 180;
		private const DELETE_BUTTON_X_STARTLOC:int = 120;		//Button to delete data
		private const DELETE_BUTTON_Y_STARTLOC:int = 350;
		private const CONFIRM_DEL_BUTTON_X_STARTLOC:int = 250;	//Button to confirm delete
		private const CONFIRM_DEL_BUTTON_Y_STARTLOC:int = 300;
		private const CANCEL_DEL_BUTTON_X_STARTLOC:int = 470;	//Button to cancel delete
		private const CANCEL_DEL_BUTTON_Y_STARTLOC:int = 300;
		//private const LOADGAME_TXT_X:int = 0;					//X Coordinate for loadgame txt
		//private const LOADGAME_TXT_Y:int = 0;					//y Coordinate for loadgame txt
		
		private var txtLabel:TextField;
		
		private var mainRef:Main;
		private var thisStage:Stage;
		
		//private var menuButtonImgData:ImageData;
		//private var load1ButtonImgData:ImageData;
		//private var load2ButtonImgData:ImageData;
		//private var load3ButtonImgData:ImageData;
		//private var load4ButtonImgData:ImageData;
		//private var deleteButtonImgData:ImageData;
		//private var confirmDeleteButtonImgData:ImageData;
		//private var cancelDeleteButtonImgData:ImageData;
		private var confirmDelImgData:Vector.<ImageData>;
		
		//private var menuButton:MenuButton;						//button to return to start menu
		private var loadButtons:Vector.<MenuButton>;			//4 load buttons
		private var confirmDelButtons:Vector.<GenButton>;		//4 buttons to confirm whether to delete data slot
		//private var deleteButton:MenuButton;
		private var confirmDeleteButton:GenButton;
		private var cancelDeleteButton:GenButton;

		private var whichDataSlotToDelete:int;
		
		private var CaptainSelectScreen:Captain_Select;
		
		//private var LoadSound:ShipCreak;
		//private var sounds:Array;
		
		public function LoadMenu(mainRef:Main) 
		{
			txtLabel = new TextField();
			this.mainRef = mainRef;
			thisStage = mainRef.stage;
			
			loadButtons = new Vector.<MenuButton>;				//give the VECTOR of menu buttons memory
			
			confirmDelButtons = new Vector.<GenButton>;
			confirmDelImgData = new Vector.<ImageData>;
			
			
			CaptainSelectScreen = new Captain_Select();
			
			//these image data objects don't have "labels" because they get set in Initialize()
			//load1ButtonImgData = new ImageData(LOADGAME_1_BUTTON_X_STARTLOC, LOADGAME_1_BUTTON_Y_STARTLOC, "");
			//load2ButtonImgData = new ImageData(LOADGAME_2_BUTTON_X_STARTLOC, LOADGAME_2_BUTTON_Y_STARTLOC, "");
			//load3ButtonImgData = new ImageData(LOADGAME_3_BUTTON_X_STARTLOC, LOADGAME_3_BUTTON_Y_STARTLOC, "");
			//load4ButtonImgData = new ImageData(LOADGAME_4_BUTTON_X_STARTLOC, LOADGAME_4_BUTTON_Y_STARTLOC, "");
			confirmDelImgData[0] = new ImageData(CaptainSelectScreen.slot1.x, CaptainSelectScreen.slot1.y, "Delete game 1?");
			confirmDelImgData[1] = new ImageData(CaptainSelectScreen.slot2.x, CaptainSelectScreen.slot2.y, "Delete game 2?");
			confirmDelImgData[2] = new ImageData(CaptainSelectScreen.slot3.x, CaptainSelectScreen.slot3.y, "Delete game 3?");
			confirmDelImgData[3] = new ImageData(CaptainSelectScreen.slot4.x, CaptainSelectScreen.slot4.y, "Delete game 4?");
			//deleteButtonImgData = new ImageData(DELETE_BUTTON_X_STARTLOC, DELETE_BUTTON_Y_STARTLOC, "Delete data");
			//menuButtonImgData = new ImageData(MENU_BUTTON_X_STARTLOC, MENU_BUTTON_Y_STARTLOC, "Back");
			//confirmDeleteButtonImgData = new ImageData(CONFIRM_DEL_BUTTON_X_STARTLOC, CONFIRM_DEL_BUTTON_Y_STARTLOC, "Confirm");
			//cancelDeleteButtonImgData = new ImageData(CANCEL_DEL_BUTTON_X_STARTLOC, CANCEL_DEL_BUTTON_Y_STARTLOC, "Cancel");
			//
			//loadButtons[0] = new MenuButton(load1ButtonImgData, LoadGame1BtnInputMouseClick);
			//loadButtons[1] = new MenuButton(load2ButtonImgData, LoadGame2BtnInputMouseClick);
			//loadButtons[2] = new MenuButton(load3ButtonImgData, LoadGame3BtnInputMouseClick);
			//loadButtons[3] = new MenuButton(load4ButtonImgData, LoadGame4BtnInputMouseClick);
			//confirmDelButtons[0] = new MenuButton(confirmDelImgData[0], DeleteGame1BtnInputMouseClick);
			//confirmDelButtons[1] = new MenuButton(confirmDelImgData[1], DeleteGame2BtnInputMouseClick);
			//confirmDelButtons[2] = new MenuButton(confirmDelImgData[2], DeleteGame3BtnInputMouseClick);
			//confirmDelButtons[3] = new MenuButton(confirmDelImgData[3], DeleteGame4BtnInputMouseClick);
			//deleteButton = new MenuButton(deleteButtonImgData, DeleteBtnInputMouseClick);
			//menuButton = new MenuButton(menuButtonImgData, MenuBtnInputMouseClick);
			CaptainSelectScreen.main_button.addEventListener(MouseEvent.CLICK, MenuBtnInputMouseClick);
			//confirmDeleteButton = new MenuButton(confirmDeleteButtonImgData, ConfirmDeleteBtnInputMouseClick);
			//cancelDeleteButton = new MenuButton(cancelDeleteButtonImgData, CancelDeleteBtnInputMouseClick);
			
			
			/**********************
			 * This is stuff taken out and replaces by the menu generated in the SWC
			//text formats
			var _loadTxtFormat:TextFormat = new TextFormat();
			_loadTxtFormat.size = 15;
			_loadTxtFormat.font = "Arial";
			_loadTxtFormat.color = 0x000000;
			
			_loadTxtFormat.align = TextFormatAlign.RIGHT;
			_loadTxtFormat.rightMargin = 6;
			
			//the buttons
			//these loadButtons don't have "labels" yet because they get set in Initialize()
			loadButtons[0] = new GenButton(LOADGAME_1_BUTTON_X_STARTLOC, LOADGAME_1_BUTTON_Y_STARTLOC, DiceTable, LoadGame1BtnInputMouseClick, "", _loadTxtFormat);
			loadButtons[1] = new GenButton(LOADGAME_2_BUTTON_X_STARTLOC, LOADGAME_2_BUTTON_Y_STARTLOC, DiceTable, LoadGame2BtnInputMouseClick, "", _loadTxtFormat);
			loadButtons[2] = new GenButton(LOADGAME_3_BUTTON_X_STARTLOC, LOADGAME_3_BUTTON_Y_STARTLOC, DiceTable, LoadGame3BtnInputMouseClick, "", _loadTxtFormat);
			loadButtons[3] = new GenButton(LOADGAME_4_BUTTON_X_STARTLOC, LOADGAME_4_BUTTON_Y_STARTLOC, DiceTable, LoadGame4BtnInputMouseClick, "", _loadTxtFormat);
			
			
			

			deleteButton = new GenButton(DELETE_BUTTON_X_STARTLOC, DELETE_BUTTON_Y_STARTLOC, ScrollButton, DeleteBtnInputMouseClick, "Delete Data");
			menuButton = new GenButton(MENU_BUTTON_X_STARTLOC, MENU_BUTTON_Y_STARTLOC, Round_Button, MenuBtnInputMouseClick);
			
			//the background
			var _bgImg:Med_Scroll_BG = new Med_Scroll_BG();
			_bgImg.width = 800;
			_bgImg.height = 480;
			addChild(_bgImg);
			graphics.beginFill(COLOR);							//draw the background
			graphics.drawRect(0, 0, WIDTH, HEIGHT);
			graphics.endFill();
			
			
			//add the load buttons (with their character bust image)
			var _loadBtnImgs:Vector.<Character_Bust> = new Vector.<Character_Bust>();
			//scrollbutton's default dimensions is 204.95 x 110. width/height ratio is (204.95/110)
			for (var i:int = 0; i < 4; i++) {
				_loadBtnImgs[i] = new Character_Bust();
				_loadBtnImgs[i].gotoAndStop(i + 1);
				var _ratio:Number = _loadBtnImgs[i].width / _loadBtnImgs[i].height;
				//var _height:Number = loadButtons[i].height;
				var _height:Number = 90;
				_loadBtnImgs[i].height = _height;
				_loadBtnImgs[i].width = _height * _ratio;
				loadButtons[i].addChild(_loadBtnImgs[i]);		//add the bust to the button
				addChild(loadButtons[i]);						//now add the whole button
			}
			//add the other buttons
			addChild(deleteButton);
			addChild(menuButton);
			
			txtLabel.text = "Load Menu";
			addChild(txtLabel);
			
			
			*/
			//loadButtons[0] = CaptainSelectScreen.slot1;
			//loadButtons[1] = CaptainSelectScreen.slot2;
			//loadButtons[2] = CaptainSelectScreen.slot3;
			//loadButtons[3] = CaptainSelectScreen.slot4;
			confirmDelButtons[0] = new GenButton(CaptainSelectScreen.slot1.x, CaptainSelectScreen.slot1.y, DiceTable, DeleteGame1BtnInputMouseClick, "Delete\ngame 1?");
			confirmDelButtons[1] = new GenButton(CaptainSelectScreen.slot2.x, CaptainSelectScreen.slot2.y, DiceTable, DeleteGame2BtnInputMouseClick, "Delete\ngame 2?");
			confirmDelButtons[2] = new GenButton(CaptainSelectScreen.slot3.x, CaptainSelectScreen.slot3.y, DiceTable, DeleteGame3BtnInputMouseClick, "Delete\ngame 3?");
			confirmDelButtons[3] = new GenButton(CaptainSelectScreen.slot4.x, CaptainSelectScreen.slot4.y, DiceTable, DeleteGame4BtnInputMouseClick, "Delete\ngame 4?");
			confirmDeleteButton = new GenButton(CONFIRM_DEL_BUTTON_X_STARTLOC, CONFIRM_DEL_BUTTON_Y_STARTLOC, ScrollButton, ConfirmDeleteBtnInputMouseClick, "Confirm");
			cancelDeleteButton = new GenButton(CANCEL_DEL_BUTTON_X_STARTLOC, CANCEL_DEL_BUTTON_Y_STARTLOC, ScrollButton, CancelDeleteBtnInputMouseClick, "Cancel");
			
			addChild(CaptainSelectScreen);
		}
		
		//Initialize()
		//initializes the Load menu... make sure to call it to refresh recently saved data
		public function Initialize():void
		{
			var _btnLabel:String;
			var _savedData:SharedObject;
			
			
			for (var i:int = 1; i <= 4; i++)
			{
				_savedData = SharedObject.getLocal("PirateGameSaveData" + i);
				if (_savedData.data.hasData == true) {
					_btnLabel = "Money: $" + _savedData.data.totalMoney + 
								"\nAchievements: " + _savedData.data.achievementRcvdIdx.length +
								"\nPlaytime: " + TimeFormat.formatTime(_savedData.data.gameTime/1000) + 
								"\nSaved: " + _savedData.data.timeStamp;
								
					CaptainSelectScreen["slot" + i].coin_txt.text = "= $" + _savedData.data.totalMoney;
					CaptainSelectScreen["slot" + i].ach_txt.text = ": " + _savedData.data.achievementRcvdIdx.length + "/31";
					CaptainSelectScreen["slot" + i].date_txt.text = _savedData.data.timeStamp;		
				} else {
					_btnLabel = "New Game";
					
					CaptainSelectScreen["slot" + i].coin_txt.text = " = $0";
					CaptainSelectScreen["slot" + i].ach_txt.text = ": 0/31";
					CaptainSelectScreen["slot" + i].date_txt.text = "New Game";	
					
				}
				//loadButtons[i - 1].SetButtonLabel(_btnLabel, LOADGAME_TXT_X, LOADGAME_TXT_Y);
				
				CaptainSelectScreen["slot" + i].addEventListener(MouseEvent.CLICK, this["LoadGame" + i + "BtnInputMouseClick"]);
			}
			
			CaptainSelectScreen.del_button.addEventListener(MouseEvent.CLICK, DeleteBtnInputMouseClick);
			InitImage();
		}
		//InitImage()
		//Initializes the image of the Menu
		protected override function InitImage():void
		{
			super.InitImage();
		}
		
		//DisableAllButtonsWhileConfirmingDelete()
		//disables all the buttons except the confirm/cancel-delete buttons
		private function DisableAllButtonsWhileConfirmingDelete():void
		{
			for (var i:int = 0; i < 4; i++) {
				//loadButtons[i].DisableButton();
				CaptainSelectScreen["slot" + (i+1)].removeEventListener(MouseEvent.CLICK, this["LoadGame" + (i+1) + "BtnInputMouseClick"]);
				confirmDelButtons[i].DisableButton();
			}
			//deleteButton.DisableButton();
			CaptainSelectScreen.del_button.removeEventListener(MouseEvent.CLICK, DeleteBtnInputMouseClick);
			CaptainSelectScreen.del_button.removeEventListener(MouseEvent.CLICK, CancelDeleteBtnInputMouseClick2)
			CaptainSelectScreen.main_button.removeEventListener(MouseEvent.CLICK, MenuBtnInputMouseClick);
			//menuButton.DisableButton();
		}
		private function ReEnableAllButtons():void
		{
			for (var i:int = 0; i < 4; i++) {
				//loadButtons[i].EnableButton();
				CaptainSelectScreen["slot" + (i+1)].addEventListener(MouseEvent.CLICK, this["LoadGame" + (i+1) + "BtnInputMouseClick"]);
				confirmDelButtons[i].EnableButton();
			}
			//deleteButton.EnableButton();
			//menuButton.EnableButton();
			CaptainSelectScreen.del_button.addEventListener(MouseEvent.CLICK, DeleteBtnInputMouseClick);
			CaptainSelectScreen.main_button.addEventListener(MouseEvent.CLICK, MenuBtnInputMouseClick);

		}
		
		//MenuBtnInputMouseClick()
		//called on back menu button mouse click
		protected function MenuBtnInputMouseClick(e:MouseEvent):void
		{
			mainRef.ReturnToStartMenu(this);
		}
		
		protected function LoadGame1BtnInputMouseClick(e:MouseEvent):void {
			mainRef.playerIndex = 1;
			mainRef.LoadGame(this, SharedObject.getLocal("PirateGameSaveData1"));
		}
		protected function LoadGame2BtnInputMouseClick(e:MouseEvent):void {
			mainRef.playerIndex = 2;
			mainRef.LoadGame(this, SharedObject.getLocal("PirateGameSaveData2"));
		}
		protected function LoadGame3BtnInputMouseClick(e:MouseEvent):void {
			mainRef.playerIndex = 3;
			mainRef.LoadGame(this, SharedObject.getLocal("PirateGameSaveData3"));
		}
		protected function LoadGame4BtnInputMouseClick(e:MouseEvent):void {
			mainRef.playerIndex = 4;
			mainRef.LoadGame(this, SharedObject.getLocal("PirateGameSaveData4"));
		}
		
		//ConfirmDeleteBtnInputMouseClick()
		//delete the selected load-data, and do the same actions that cancel would do
		protected function ConfirmDeleteBtnInputMouseClick(e:MouseEvent = null):void {
			mainRef.DeleteGame(whichDataSlotToDelete);
			CancelDeleteBtnInputMouseClick();
		}
		//CancelDeleteBtnInputMouseClick()
		//remove confirm/cancel buttons,
		//then do all the normal things
		protected function CancelDeleteBtnInputMouseClick(e:MouseEvent = null):void {
			removeChild(confirmDeleteButton);
			removeChild(cancelDeleteButton);
			CancelDeleteBtnInputMouseClick2();
		}
		//CancelDeleteBtnInputMouseClick2()
		//change back load buttons, 
		//re-enable the buttons that were disabled, and change the deleteButton's click-function back
		private function CancelDeleteBtnInputMouseClick2(e:MouseEvent = null):void {
			for (var i:int = 0; i < 4; i++) {
				removeChild(confirmDelButtons[i]);
			}
			ReEnableAllButtons();
			//deleteButton.ChangeIMCFunc(DeleteBtnInputMouseClick);
			CaptainSelectScreen.del_button.addEventListener(MouseEvent.CLICK, DeleteBtnInputMouseClick);
			CaptainSelectScreen.del_button.removeEventListener(MouseEvent.CLICK, CancelDeleteBtnInputMouseClick2);
		}
		
		//DeleteBtnInputMouseClick()
		//gets passed into MenuButton class for deleteButton
		//re-clicking it will cancel the delete action
		protected function DeleteBtnInputMouseClick(e:MouseEvent):void {
			for (var i:int = 0; i < 4; i++) {
				addChild(confirmDelButtons[i]);				//overlay 4 new "delete game" buttons
			}
			//deleteButton.ChangeIMCFunc(CancelDeleteBtnInputMouseClick2);
			CaptainSelectScreen.del_button.addEventListener(MouseEvent.CLICK, CancelDeleteBtnInputMouseClick2);
		}
		
		//DeleteGame1BtnInputMouseClick()
		//store which data slot was selected as potential delete candidate,
		//display the confirm/cancel delete buttons, and disable all the rest
		protected function DeleteGame1BtnInputMouseClick():void {
			whichDataSlotToDelete = 1;
			addChild(confirmDeleteButton);
			addChild(cancelDeleteButton);
			DisableAllButtonsWhileConfirmingDelete();
		}
		protected function DeleteGame2BtnInputMouseClick():void {
			whichDataSlotToDelete = 2;
			addChild(confirmDeleteButton);
			addChild(cancelDeleteButton);
			DisableAllButtonsWhileConfirmingDelete();
		}
		protected function DeleteGame3BtnInputMouseClick():void {
			whichDataSlotToDelete = 3;
			addChild(confirmDeleteButton);
			addChild(cancelDeleteButton);
			DisableAllButtonsWhileConfirmingDelete();
		}
		protected function DeleteGame4BtnInputMouseClick():void {
			whichDataSlotToDelete = 4;
			addChild(confirmDeleteButton);
			addChild(cancelDeleteButton);
			DisableAllButtonsWhileConfirmingDelete();
		}
		
		
	}

}